//
//  ConfirmBox.cpp
//  client
//
//  Created by DuanHongbo on 15/12/21.
//
//

#include "ConfirmBox.h"

USING_NS_CC;
using namespace cocos2d::experimental;
using namespace cocos2d::ui;

bool ConfirmBox::init(){
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();

    auto rootNode = CSLoader::createNode("ui2/With_choose.csb");
    
    if(rootNode){
        rootNode->setContentSize(Size(visibleSize.width,visibleSize.height));
        
        this->addChild(rootNode);
        cocos2d::ui::Helper::doLayout(rootNode);
        
        auto tipsNode = rootNode->getChildByName("tips");
        if(tipsNode){
            Text* messageLabel = dynamic_cast<Text*>(tipsNode->getChildByName("tips_txt"));
            if(messageLabel){
                messageLabel->setString("");
            }
            _messageLabel = messageLabel;
            
            Button* confirmButton = dynamic_cast<Button*>(tipsNode->getChildByName("Button_queding"));
            if(confirmButton){
                confirmButton->addClickEventListener([this](Ref* sender){
                    if(this->_confirmCallback){
                        this->_confirmCallback(this);
                    }
                });
                
                _confirmLabel = dynamic_cast<Text*>(confirmButton->getChildByName("name"));
            }
            
            Button* cancelButton = dynamic_cast<Button*>(tipsNode->getChildByName("Button_quxiao"));
            if(cancelButton){
                cancelButton->addClickEventListener([this](Ref* sender){
                    if(this->_cancelCallback){
                        this->_cancelCallback(this);
                    }
                });
                 _cancelLabel = dynamic_cast<Text*>(cancelButton->getChildByName("name"));
            }
        }
    
        _rootNode = rootNode;
        
        auto timeline = CSLoader::createTimeline("ui2/With_choose.csb");
        if(timeline){
            timeline->gotoFrameAndPlay(0,false);
            rootNode->runAction(timeline);
        }
    }

    return true;
}


void ConfirmBox::setTitle(const char* str)
{
    
}

void ConfirmBox::setMessage(const char* str)
{
    if(_messageLabel){
        _messageLabel->setString(str);
    }
}

void ConfirmBox::setConfirmButton(const char* str)
{
    if(_confirmLabel){
        _confirmLabel->setString(str);
    }
}

void ConfirmBox::setCancelButton(const char* str)
{
    if(_cancelLabel){
        _cancelLabel->setString(str);
    }
}

void ConfirmBox::setConfirmCallback(const ButtonClickDelegate& cb)
{
    _confirmCallback = cb;
}

void ConfirmBox::setCancelCallback(const ButtonClickDelegate& cb)
{
    _cancelCallback = cb;
}
